//
// Created by gyc on 2020/10/27.
//
#include "../../common.h"

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        stack<TreeNode *> st{};
        vector<int> res{};
        if (root == nullptr) {
            return res;
        }
        st.push(root);
        while (!st.empty()) {
            auto curr = st.top();
            st.pop();
            res.emplace_back(curr->val);
            st.push(curr->right);
            st.push(curr->left);
        }
        return res;
    }
};